<template>
  <div ref="goodslist">
    <!-- 瀑布流布局 -->
    <waterfall
      :col="2"
      :data="spuList"
      :loadDistance="1"
      @loadmore="loadmore()"
      class="aaaasadas"
    >
      <template>
        <div class="goodslist" v-for="(item, index) in spuList" :key="index">
          <router-link :to="{ name: 'goods', params: { id: item.id } }">
            <img :src="item.img" />
            <p class="goodslist-title">{{ item.title }}</p>
            <div class="goodslist-tags-out">
              <van-tag
                v-for="(item2, index) in item.tags"
                :key="index"
                type="primary"
                class="goodslist-tags"
              >
                {{ item2 }}
              </van-tag>
            </div>
            <!-- 价格 -->
            <p class="goodslist-price">￥{{ item.price }}</p>

            <p class="goodslist-subtitle">{{ item.description }}</p>
          </router-link>
        </div>
      </template>
    </waterfall>
  </div>
</template>

<script>
export default {
  name: "GoodsList",
  data() {
    return {
      spuList: [],
      // 每次分页请求的数量
      pageSize: 5,
      // 本次分页请求的起始计数值
      pageStart: 0,
      stillLoading: true,
    };
  },
  methods: {
    getSpuByPage() {
      this.$http
        .get("http://localhost:8080/spu/page", {
          params: {
            // 每页查询数量（每发一次请求，有几个商品）
            count: this.pageSize,
            // 起始数（商品从第几个开始）
            start: this.pageStart,
          },
        })
        .then((res) => {
          // 得到当前分页结果的列表数据
          let currentPageResult = res.data.list;

          // 对每个商品的tags字段进行处理，按$分割
          currentPageResult.forEach((item) => {
            if (item.tags && item.tags !== "") {
              item.tags = item.tags.split("$");
            }
            // 如果没有图片，显示默认图片
            if (item.img == null) {
              item.img =
                "https://img0.baidu.com/it/u=3298869242,4148402969&fm=253&fmt=auto&app=138&f=JPG?w=350&h=245";
            }
          });
          // 将分页结果添加到整个商品列表中
          this.spuList.push(...currentPageResult);

          // 根据分页结果的数量，判断是否到底
          if (currentPageResult.length < this.pageSize) {
            this.stillLoading = false;
          }
        });
    },
    loadmore() {
      if (!this.stillLoading) {
        return;
      }
      this.pageStart += this.pageSize;
      this.getSpuByPage();
    },
  },
  mounted() {
    this.getSpuByPage();
  },
};
</script>

<style scoped>


@media screen and (max-width: 800px) {
  .goodslist {
    background: white;
    text-align: left;
    float: left;
    width: 170px;
    margin: 8px 18px;
    padding-bottom: 10px;
    box-shadow: 0 0 4px rgb(64, 44, 47);
    border-radius: 8px 8px 8px 8px;
    display: flex;
  }

  .goodslist img {
    border-radius: 8px 8px 0 0;
    width: 100%;
    max-width: 100%;
    max-height: 100%;
  }
}

@media screen and (min-width: 820px) {
  .goodslist {
    background: white;
    text-align: left;
    float: left;
    width: 96%;
    margin: 8px 8px;
    padding-bottom: 10px;
    box-shadow: 0 0 4px rgb(64, 44, 47);
    border-radius: 8px 8px 8px 8px;
  }

  .goodslist img {
    border-radius: 8px 8px 0 0;
    width: 100%;
    max-width: 100%;
    max-height: 100%;
  }
}

.goodslist-subtitle {
  word-wrap: break-word;
  padding: 5px 10px 0 11px;
  color: rgb(95, 126, 151);
  font-size: 12px;
}

.goodslist-background {
  height: 500px;
  background-color: rgb(169, 221, 187);
}

.goodslist-title {
  font-size: 15px;
  color: black;
  padding: 2px 10px 0 11px;
}

.goodslist-tags {
  background: rgb(201, 174, 115);
  font-size: 5px;
  padding-top: 2px;
  margin-top: 2px;
  margin-left: 4px;
  color: white;
}

.goodslist-tags-out {
  margin-left: 6px;
}

.goodslist-price {
  padding-top: 5px;
  color: rgb(62, 62, 131);
  padding-left: 10px;
}
</style>